package com.cy.common.exception;

import com.cy.common.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.MyBatisSystemException;
import org.omg.CORBA.SystemException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author : 残影
 * @Date: 2022/4/12
 */
@Slf4j
@ControllerAdvice
public class BaseExceptionHandler {
    /**
     * 处理自定义异常
     */
    @ResponseBody
    @ExceptionHandler(CommonException.class)
    public Result handleRRException(CommonException e) {
        Result r = new Result();
        r.put("code", e.getCode());
        r.put("msg", e.getMessage());
        List<StackTraceElement> traceElements = Arrays.stream(e.getStackTrace()).limit(1).collect(Collectors.toList());
        log.error(e.getMessage());
        System.out.println(traceElements.get(0));
        return r;
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseBody
    public Result handlerNoFoundException(Exception e) {
        log.error(e.getMessage(), e);
        return Result.failed(404, "路径不存在，请检查路径是否正确");
    }

    @ResponseBody
    @ExceptionHandler(DuplicateKeyException.class)
    public Result handleDuplicateKeyException(DuplicateKeyException e) {
        log.error(e.getMessage(), e);
        return Result.failed("数据库中已存在该记录");
    }

    @ResponseBody
    @ExceptionHandler(NullPointerException.class)
    public Result handleNullPointerException(NullPointerException e) {
        log.error(e.getMessage(), e);
        return Result.failed("空指针异常");
    }

    @ResponseBody
    @ExceptionHandler(ClassCastException.class)
    public Result handleClassCastException(ClassCastException e) {
        log.error(e.getMessage(), e);
        return Result.failed("类型强制转换异常");
    }

    @ResponseBody
    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    public Result handleArrayIndexOutOfBoundsException(ArrayIndexOutOfBoundsException e) {
        log.error(e.getMessage(), e);
        return Result.failed("数组下标越界异常");
    }

    @ResponseBody
    @ExceptionHandler(EOFException.class)
    public Result handleEOFException(EOFException e) {
        log.error(e.getMessage(), e);
        return Result.failed("文件已结束异常");
    }

    @ResponseBody
    @ExceptionHandler(FileNotFoundException.class)
    public Result handleFileNotFoundException(FileNotFoundException e) {
        log.error(e.getMessage(), e);
        return Result.failed("文件未找到异常");
    }

    @ResponseBody
    @ExceptionHandler(NumberFormatException.class)
    public Result handleNumberFormatException(NumberFormatException e) {
        log.error(e.getMessage(), e);
        return Result.failed("字符串转换为数字异常");
    }

    @ResponseBody
    @ExceptionHandler(SQLException.class)
    public Result handleSQLException(SQLException e) {
        log.error(e.getMessage(), e);
        return Result.failed("操作数据库异常");
    }

    @ResponseBody
    @ExceptionHandler(IOException.class)
    public Result handleIOException(IOException e) {
        log.error(e.getMessage(), e);
        return Result.failed("输入输出异常");
    }

    @ResponseBody
    @ExceptionHandler(NoSuchMethodException.class)
    public Result handleNoSuchMethodException(NoSuchMethodException e) {
        log.error(e.getMessage(), e);
        return Result.failed("方法未找到异常");
    }

    @ExceptionHandler(SystemException.class)
    public Result handleSystemException(SystemException e) {
        log.error(e.getMessage(), e);
        return Result.failed("系统异常");
    }

    @ResponseBody
    @ExceptionHandler(NegativeArraySizeException.class)
    public Result handleNegativeArraySizeException(NegativeArraySizeException e) {
        log.error(e.getMessage(), e);
        return Result.failed("创建一个大小为负数的数组错误异常");
    }

    @ResponseBody
    @ExceptionHandler(IllegalStateException.class)
    public Result handleIllegalStateException(IllegalStateException e) {
        log.error(e.getMessage(), e);
        return Result.failed("请求状态异常");
    }

    @ResponseBody
    @ExceptionHandler(UnsupportedOperationException.class)
    public Result handleUnsupportedOperationException(UnsupportedOperationException e) {
        log.error(e.getMessage(), e);
        return Result.failed("不支持的操作异常");
    }

    @ResponseBody
    @ExceptionHandler(InvocationTargetException.class)
    public Result handleInvocationTargetException(InvocationTargetException e) {
        log.error(e.getMessage(), e);
        return Result.failed("反射异常");
    }

    @ResponseBody
    @ExceptionHandler(IllegalArgumentException.class)
    public Result handleIllegalArgumentException(IllegalArgumentException e) {
        log.error(e.getMessage(), e);
        return Result.failed("非法参数异常");
    }

    @ResponseBody
    @ExceptionHandler(MyBatisSystemException.class)
    public Result handleMyBatisSystemException(MyBatisSystemException e) {
        log.error(e.getMessage(), e);
        return Result.failed("Mybatis系统异常");
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.error(e.getMessage(), e);
        return Result.failed();
    }
}
